package thread;

/**
 * @author HuangJin
 * @version 1.0
 * describe:线程的另一种创建方式：
 * 实现Runnable接口单独定义线程任务
 * @date 2023/3/14 10:30
 */
public class ThreadDemo2 {
    public static void main(String[] args) {
        //单独实例化任务
        Runnable r1 = new MyRunnable1();
        Runnable r2 = new MyRunnable2();

        //创建线程并指派任务
        Thread t1 = new Thread(r1);
        Thread t2 = new Thread(r2);
        t1.start();
        t2.start();

    }
}

/**
 * 单独定义线程任务是实现Runnable接口（或者后期在线程池中实现Callable接口）
 * 由于是实现接口，不再是直接继承
 */
class MyRunnable1 implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i <1000;i++ ) {
            System.out.println("你是谁呀？");
        }

    }
}
class MyRunnable2 implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i <1000;i++ ) {
            System.out.println("开门，查水表的！");
        }
    }
}